<?php

use common\components\Migration;
use common\modules\wechat\models\Account;
use common\modules\wechat\models\Fans;
use common\modules\wechat\models\Media;
use common\modules\wechat\models\MessageHistory;
use common\modules\wechat\models\Module;
use common\modules\wechat\models\MpUser;
use common\modules\wechat\models\ReplyRule;
use common\modules\wechat\models\ReplyRuleKeyword;
use yii\db\Schema;

class m150217_131753_init_data extends Migration
{

    /**
     * {@inheritdoc}
     */
    public function safeUp()
    {
        $this->initWechatTable();
        $this->initModuleTable();
        $this->initReplyRuleTable();
        $this->initUserTable();
        $this->initFansTable();
        $this->initMessageHistoryTable();
        $this->initMediaTable();
    }

    /**
     * 公众号表
     */
    public function initWechatTable()
    {
        $rows = [
            [
                'name' => '测试公众号1',
                'account' => 'tourze-test-1',
                'original' => 'gh0001',
                'type' => Account::TYPE_SERVICE,
                'key' => Yii::$app->security->generateRandomString(16),
                'secret' => Yii::$app->security->generateRandomString(32),
                'description' => 'Desc',
                'username' => 'test1@tourze.com',
            ],
            [
                'name' => '测试公众号2',
                'account' => 'tourze-test-2',
                'original' => 'gh0001',
                'type' => Account::TYPE_SERVICE_VERIFY,
                'key' => Yii::$app->security->generateRandomString(16),
                'secret' => Yii::$app->security->generateRandomString(32),
                'description' => 'Desc',
                'username' => 'test1@tourze.com',
            ],
            [
                'name' => '测试公众号3',
                'account' => 'tourze-test-3',
                'original' => 'gh0001',
                'type' => Account::TYPE_SUBSCRIBE,
                'key' => Yii::$app->security->generateRandomString(16),
                'secret' => Yii::$app->security->generateRandomString(32),
                'description' => 'Desc',
                'username' => 'test3@tourze.com',
            ],
        ];

        foreach ($rows as $row)
        {
            $model = new Account;
            $model->setAttributes($row);
            if ( ! $model->save())
            {
                //print_r($model->errors);die();
            }
        }
    }

    /**
     * 扩展模块表
     */
    public function initModuleTable()
    {
    }

    /**
     * 回复规则表
     */
    public function initReplyRuleTable()
    {
    }

    /**
     * 粉丝表
     */
    public function initFansTable()
    {
    }

    /**
     * 粉丝用户表
     */
    public function initUserTable()
    {
        $rows = [
            [
                'name' => '测试公众号1',
                'account' => 'tourze-test-1',
                'original' => 'gh0001',
                'type' => Account::TYPE_SERVICE,
                'key' => Yii::$app->security->generateRandomString(16),
                'secret' => Yii::$app->security->generateRandomString(32),
                'description' => 'Desc',
                'username' => 'test1@tourze.com',
            ],
            [
                'name' => '测试公众号2',
                'account' => 'tourze-test-2',
                'original' => 'gh0001',
                'type' => Account::TYPE_SERVICE_VERIFY,
                'key' => Yii::$app->security->generateRandomString(16),
                'secret' => Yii::$app->security->generateRandomString(32),
                'description' => 'Desc',
                'username' => 'test1@tourze.com',
            ],
            [
                'name' => '测试公众号3',
                'account' => 'tourze-test-3',
                'original' => 'gh0001',
                'type' => Account::TYPE_SUBSCRIBE,
                'key' => Yii::$app->security->generateRandomString(16),
                'secret' => Yii::$app->security->generateRandomString(32),
                'description' => 'Desc',
                'username' => 'test3@tourze.com',
            ],
        ];

        foreach ($rows as $row)
        {
            $model = new MpUser;
            $model->load($row, '');
            if ( ! $model->save())
            {
                //print_r($model->errors);die();
            }
        }
    }

    /**
     * 消息记录表
     */
    public function initMessageHistoryTable()
    {
    }

    /**
     * 素材表
     */
    public function initMediaTable()
    {
    }
}